<?php

namespace app\merchant\controller;

use think\Controller;
use think\Request;
use think\Session;

class Coupon extends Common
{
    protected $coupon;

    public function _initialize()
    {
        parent::_initialize();
        $this->coupon = new \app\common\model\Coupon();
    }

    /**
     * 卡券列表页面
     */
	public function listCoupon(){
        $merchant_id = session('merchant.merchant_id');
        $page = input('page/n',1);
        $store_info = $this->coupon->getStoreinfo($merchant_id);
        $pagesize = input('DataTables_Table_0_length/n', 10);
	    $list=$this->coupon->getStoreCouponList($store_info['store_id'],$pagesize);
	    $this->assign([
	        'coupon_list' => $list,
            'page' => $page,
        ]);
		return view('couponlist');
	}

    /**
     * 添加卡券页面
     */
	public function addCoupon(){
        $merchant_id = session('merchant.merchant_id');
        $storeinfo = $this->coupon->getStoreInfo($merchant_id);
	    $this->assign([
	        'storeinfo'=>$storeinfo,
        ]);
		return view("addcoupon");
	}

    /**
     * 添加卡券数据
     */
	public function createCoupon(){
        if(Request::instance()->method() == 'POST'){
            $req_data = Request::instance()->param();
            $req_data = $req_data['data'];
            $store_info = $this->coupon->getStoreinfo(session('merchant.merchant_id'));
            $req_data['create_id'] = $store_info['store_id'];
            $req_data['activity_start_time'] = strtotime($req_data['activity_start_time']." 00:00:00");
            $req_data['activity_end_time'] = strtotime($req_data['activity_end_time']." 23:59:59");
            if(!empty($req_data['effect_time'])){
                $req_data['effect_time'] = strtotime($req_data['effect_time']." 00:00:00");
            }
            $res = $this->coupon->insertCoupon($req_data);
            if($res !== false){
                echo json_encode(array('status'=>1,'msg'=>"添加成功"));
            }else{
                echo json_encode(array('status'=>0,'msg'=>"添加失败"));
            }
            exit();
        }else{
            echo json_encode(array('status'=>0,'msg'=>"数据请求错误"));
        }
    }

    /**
     * 更新卡券数据
     */
    public function updateCoupon(){
        if(Request::instance()->method() == 'POST'){
            $req_data = Request::instance()->param();
            $req_data = $req_data['data'];
            $req_data['activity_start_time'] = strtotime($req_data['activity_start_time']." 00:00:00");
            $req_data['activity_end_time'] = strtotime($req_data['activity_end_time']." 23:59:59");
            if(!empty($req_data['effect_time'])){
                $req_data['effect_time'] = strtotime($req_data['effect_time']." 00:00:00");
            }
            $res = $this->coupon->updateCoupon($req_data);
            if($res !== false){
                echo json_encode(array('status'=>1,'msg'=>"更新成功"));
            }else{
                echo json_encode(array('status'=>0,'msg'=>"更新失败"));
            }
            exit();
        }
        $coupon_id =input("id/n");
        $couponinfo = $this->coupon->getCouponInfo($coupon_id);
        $merchant_id = session('merchant.merchant_id');
        $storeinfo = $this->coupon->getStoreInfo($merchant_id);
        $couponinfo['activity_start_time'] = date("Y-m-d",$couponinfo['activity_start_time']);
        $couponinfo['activity_end_time'] = date("Y-m-d",$couponinfo['activity_end_time']);
        //领取时间限制
        if($couponinfo['effect_time'] > 0){
            $couponinfo['effect_type'] = 1;
            $couponinfo['effect_time'] = date("Y-m-d",$couponinfo['effect_time']);
        }else{
            $couponinfo['effect_time'] = "";
            $couponinfo['effect_type'] = 0;
        }
        //用户满减
        if($couponinfo['use_minimal_amount'] > 0){
            $couponinfo['use_minimal_amount'] = intval($couponinfo['use_minimal_amount']);
            $couponinfo['limit'] = 1;
        }else{
            $couponinfo['use_minimal_amount'] = "";
            $couponinfo['limit'] = 0;
        }
        $this->assign([
            'storeinfo'=>$storeinfo,
            'couponinfo'=>$couponinfo,
        ]);
        return view('updatecoupon');
    }

    /**
     * 展示详情卡券数据
     */
    public function detailCoupon(){
        $coupon_id =input("id/n");
        $page = input('page/n',1);
        $couponinfo = $this->coupon->getCouponInfo($coupon_id);
        $merchant_id = session('merchant.merchant_id');
        $storeinfo = $this->coupon->getStoreInfo($merchant_id);
        $couponinfo['activity_start_time'] = date("Y-m-d",$couponinfo['activity_start_time']);
        $couponinfo['activity_end_time'] = date("Y-m-d",$couponinfo['activity_end_time']);
        $couponinfo['create_time'] = date("Y-m-d",$couponinfo['create_time']);
        //领取时间限制
        if($couponinfo['effect_time'] > 0){
            $couponinfo['effect_type'] = 1;
            $couponinfo['effect_time'] = date("Y-m-d",$couponinfo['effect_time']);
        }else{
            $couponinfo['effect_time'] = "";
            $couponinfo['effect_type'] = 0;
        }
        //用户满减
        if($couponinfo['use_minimal_amount'] > 0){
            $couponinfo['use_minimal_amount'] = intval($couponinfo['use_minimal_amount']);
            $couponinfo['limit'] = 1;
        }else{
            $couponinfo['use_minimal_amount'] = "";
            $couponinfo['limit'] = 0;
        }
        $this->assign([
            'storeinfo'=>$storeinfo,
            'page'=>$page,
            'couponinfo'=>$couponinfo,
        ]);
        return view('detailcoupon');
    }

    /**
     * 展示卡券用户领取情况
     */
    public function listUserCoupon(){
        $coupon_id =input("id/n");
        $usecouponinfo = $this->coupon->getUserCouponList($coupon_id);
        foreach ($usecouponinfo as $key => $value){
            $usecouponinfo[$key]['from_user_name'] = Db("user")->field("nickname")->where("user_id = {$value['from_id']}")->find();
            $usecouponinfo[$key]['from_user_name'] = $usecouponinfo[$key]['from_user_name']['nickname'];
            $usecouponinfo[$key]['use_start_time'] = date("Y-m-d",$value['use_start_time']);
            $usecouponinfo[$key]['use_end_time'] = date("Y-m-d",$value['use_end_time']);
            $usecouponinfo[$key]['create_time'] = date("Y-m-d",$value['create_time']);
            //领取时间限制
            if($value['used_time'] > 0){
                $usecouponinfo[$key]['used_time'] = date("Y-m-d",$value['used_time']);
            }else{
                $usecouponinfo[$key]['used_time'] = 0;
            }
            if($value['status'] == 1 && $value['use_end_time'] < time() ){
                Db("zj_coupon_use")->where("id = {$value['id']}")->update(['status'=>2]);
            }
        }
        $this->assign([
            'usecouponinfo'=>$usecouponinfo,
        ]);
        return view('usercouponlist');
    }

    /**
     * 卡券状态置为失效
     */
    public function deleteCoupon(){
        if(Request::instance()->method() == 'POST'){
            $id=input("id/n");
            $result = $this->coupon->delCoupon($id);
            if($result == false) {
                echo json_encode(array('status' => 0, 'msg' => "删除失败"),JSON_UNESCAPED_UNICODE);
            }else{
                echo json_encode(array('status'=> 1, 'msg' => "删除成功",'data'=>$result),JSON_UNESCAPED_UNICODE);
            }
            exit();
        }
    }

    /**
     * 用户卡券状态置为失效
     */
    public function deleteUserCoupon(){
        if(Request::instance()->method() == 'POST'){
            $id=input("id/n");
            $result = $this->coupon->delUserCoupon($id);
            if($result == false) {
                echo json_encode(array('status' => 0, 'msg' => "删除失败"),JSON_UNESCAPED_UNICODE);
            }else{
                echo json_encode(array('status'=> 1, 'msg' => "删除成功",'data'=>$result),JSON_UNESCAPED_UNICODE);
            }
            exit();
        }
    }

}
